package com.mdp.workflow.re.ctrl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdp.core.entity.Result;
import com.mdp.core.err.BizException;
import com.mdp.core.query.QueryTools;
import com.mdp.safe.client.entity.User;
import com.mdp.safe.client.utils.LoginUtils;
import com.mdp.swagger.ApiEntityParams;
import com.mdp.workflow.re.entity.Deployment;
import com.mdp.workflow.re.service.DeploymentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value="/**/re/deployment")
@Api(tags={"act_re_deployment-操作接口"})
public class DeploymentController {
	
	static Logger logger =LoggerFactory.getLogger(DeploymentController.class);
	
	@Autowired
	private DeploymentService deploymentService;

	@Value(value = "${mdp.platform-branch-id:platform-branch-001}")
	String platformBranchId;

	@ApiOperation( value = "act_re_deployment-查询列表",notes=" ")
	@ApiEntityParams(Deployment.class)
	@ApiResponses({
		@ApiResponse(code = 200,response=Deployment.class,message = "{tips:{isOk:true/false,msg:'成功/失败原因',tipscode:'错误码'},total:总记录数,data:[数据对象1,数据对象2,...]}")
	})
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public Result listDeployment(@ApiIgnore @RequestParam Map<String,Object> params){
		try {
			User user=LoginUtils.getCurrentUserInfo();
			QueryWrapper<Deployment> qw = QueryTools.initQueryWrapper(Deployment.class , params).in("tenant_id_",platformBranchId,user.getBranchId());
			IPage page=QueryTools.initPage(params);
			List<Map<String,Object>> datas = deploymentService.selectListMapByWhere(page,qw,params);
			return Result.ok("query-ok","查询成功").setData(datas).setTotal(page.getTotal());
		}catch (BizException e) {
			return Result.error(e);
		}catch (Exception e) {
			return Result.error(e);
		}
	}
	

	@ApiOperation( value = "act_re_deployment-删除",notes=" ")
	@ApiResponses({
		@ApiResponse(code = 200, message = "{tips:{isOk:true/false,msg:'成功/失败原因',tipscode:'失败时错误码'}}")
	}) 
	@RequestMapping(value="/del",method=RequestMethod.POST)
	public Result delDeployment(@RequestBody Deployment deployment){
		User user=LoginUtils.getCurrentUserInfo();
		String modelBranchId=deployment.getTenantId();
		if(StringUtils.isEmpty(modelBranchId)) {
			return Result.error("请上送机构号tenantId");
		}
		if(!user.getBranchId().equals(modelBranchId)){
			return Result.error("tenant-id-not-platform-001","tenantId","该流程属于其它机构的流程模型，或者属于公共平台共享模型，你无权删除");
		}
		if( LoginUtils.hasAnyRoles("flowAdmin","platformAdmin","branchAdmin")) {
			deploymentService.deleteByPk(deployment);
		}else{
			return Result.error("need-flowAdmin-or-platformAdmin","roles","该接口属于重大影响接口，需要流程管理员/机构管理员/平台管理员才能操作");
		};
		deploymentService.removeById(deployment);
        return Result.ok("del-ok","删除成功！");
	}


}
